class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            name: '',
            chosefile:!1,
            id: 0,
            name: "",
            fetching: !1,
            keyword: "",
            orders: 1,
            userid: "",
            batchNo:'',
             totalAmt:'',
              appRemark:'',
               totalCount:'',
            ismemberexclusive: "0",
            isnewcomers: "0",
            goodssn: "",
            specs: "",
            unit: "",
            stock: "",
            imgsrc: '',
            price: '',
            lbhtml: [],
            lbimglist: [],
            type: 0,
            isindex:"0",
            score:'',
            isscore:"0",
            path:'',
        };
    }
   // 批次号：总金额：备注：总笔数：
    submit() {
        var { totalAmt, totalCount,path } = this.state,
            that = this, err = '';
            console.log({totalAmt, totalCount,path},'1submit');
      //  if (batchNo == "") err = "请输入批次号"
        if ( totalAmt == "") err = "请输入总金额"
        if (totalCount == "") err = "请输入总笔数"
        if (path == '') err = '请上传文件'
        if (err.length > 0) {
            alert(err);
            return
        }
       console.log({totalAmt, totalCount,path},'submit');
       gzaddBatch(parseInt(Number(totalAmt)*100) , totalCount,path,res=>{
           if(res.code=='0'){
               alert('新增成功！')
               window.location.href='./list.html'
           }
       })
    }

    componentDidMount() {
        this.setState({ showloading: '' })
        let that = this, { } = that.state, typehtml = [];
        // getTypelist(res => {
        //     if (res.issuccess == 1) {
        //         if (res.list.length > 0) {
        //             typehtml.push(
        //                 <option value="0">请选择</option>
        //             )
        //             res.list.forEach((x, i) => {
        //                 typehtml.push(
        //                     <option key={i} value={x.id}>{x.name} </option>
        //                 )

        //             })
        //             this.setState({ typehtml })
        //         }
        //     }
        // })
        // let UE = window.UE
        // let ueditor = UE.getEditor('ueditor', {
        //     autoHeightEnabled: true,
        //     autoFloatEnabled: true
        // });
        // ueditor.addListener("ready", function () {
        //     that.setState({ ueditor, }, () => {
        //         let id = GetQueryString('id')
        //         if (id != 'null' && id) {
        //             that.setState({ id }, () => {
        //                 that.freshlist()
        //             })
        //         } else {
        //             that.setState({ showloading: '' })
        //         }
        //     });
        // }, 2);
    }

    freshlist() {
        let { id,ueditor } = this.state, that = this, lbimglist = [], img = [], lbhtml = [];
        getGoodsdetail(id, res => {
            this.setState({ showloading: '' })
            if (res.issuccess == 1) {
                let { } = this.state,
                    { name, detail, stock, ismemberexclusive,isindex, isnewcomers, specs, type, unit, price, imgsrc, lbimgs,goodssn,isscore,score  } = res.data
                imgsrc = `${API_HEAD}imgs/${imgsrc}`
                img = lbimgs.split(',')
                img.forEach(x => {
                    x = `${API_HEAD}imgs/${x}`
                    lbimglist.push(x)
                })
                lbimglist.forEach((x, i) => {
                    lbhtml.push(
                        <div className="lbimg">
                            <img className="img" src={x} />
                            <div className="imgmodel">
                                <div className="delbox">
                                    <img src="./src/dellb.png" onClick={this.del_img.bind(this, 'lb', i)} />
                                </div>
                            </div>
                        </div>
                    )
                })
                ueditor.setContent(that.replaceraw(decodeURIComponent(detail)))
                that.setState({ name, stock, ismemberexclusive, isnewcomers, specs, type, unit, price: (price / 100.00), name, imgsrc ,lbimglist,lbhtml,goodssn,isindex,isscore,score  })
            }
        })
    }

   
    inputchange(prop, e) {
        this.setState({ [prop]: e.target.value })
    }
    search() {
        let that = this
        that.setState({ showloading: 'ON' }, () => {
            that.getuserlist()
        })
    }
    inputchange1(prop, e) {
        // let { isrecommend } = this.state
        // [prop] = [prop] == "1" ? "0" : "1"
        this.setState({ [prop]: e == true ? "1" : "0" })
    }
    inputchange3(prop, e) {
        this.setState({ [prop]: e })
    }
  




    
    toback() {
        window.location.href = "./list.html";
    }

    uploadfile(e){
       // console.log(e,'uploadfile');
       let{chosefile} = this.state,that = this;
       if(!chosefile){
           alert('请先选择文件')
       }else{
       var form = document.getElementById('uploadthis'), 
       formData = new FormData(form); 
     $.ajax({ 
      url: `${API_HEAD}payroll/upload`, 
      type:"post", 
      data:formData, 
      processData:false, 
      contentType:false, 
      success:function(res){ 
       if(res){ 
        alert("上传成功！");
        console.log(res,'resres');
        that.setState({path:JSON.parse(res).data.oldPath})
       } 
      }, 
      error:function(err){ 
       alert("网络连接失败,稍后重试",err); 
      } 
     
     }) 
    }
    }
    // 图片
    filechangeimg(e) {
        var that = this;
        var filedata = $('#file' + e)[0].files[0];
        let _name, _fileName, filedatasize = 10240;

        _name = $('#file' + e)[0].value;
        _fileName = _name.substring(_name.lastIndexOf(".") + 1).toLowerCase();
        if (_fileName !== "png" && _fileName !== "jpg" && _fileName !== "gif" && _fileName !== "jpeg") {
            alert("上传图片格式不正确，请重新上传");
        } else {
            if (filedatasize < filedata.size / 1024) {
                alert("上传图片不能大于10M");
                return;
            }

            let filee = e
            var reader = new FileReader();
            reader.readAsDataURL(filedata);
            reader.onload = function (e) {
                var base64str = e.target.result;
                that.putswiperimglistdata([base64str], filee)
            }
        }
    }
    putswiperimglistdata(target, prop) {
        console.log(target, 'target');
        console.log(prop, 'prop');

        let { lbimglist, mpzimglist } = this.state, imgsrc = '', that = this;
        if (prop == 'imgsrc') {
            imgsrc = target[0]
            if (!isBase64(imgsrc)) {
                if (imgsrc.indexOf(`${API_HEAD}`) == -1) {
                    imgsrc = `${API_HEAD}imgs/${imgsrc}`
                }
            }
            that.setState({ [prop]: imgsrc })
        } else {
            let lbhtml = []
            imgsrc = target[0]
            if (!isBase64(imgsrc)) {
                if (imgsrc.indexOf(`${API_HEAD}`) == -1) {
                    imgsrc = `${API_HEAD}imgs/${imgsrc}`
                }
            }
            lbimglist.push(imgsrc)
            lbimglist.forEach((x, i) => {
                lbhtml.push(
                    <div className="lbimg">
                        <img className="img" src={x} />
                        <div className="imgmodel">
                            <div className="delbox">
                                <img src="./src/dellb.png" onClick={this.del_img.bind(this, 'lb', i)} />
                            </div>
                        </div>
                    </div>
                )
            })
            this.setState({ lbhtml, lbimglist })

        }

    }
    preimg() {
        $('#antdimg').click();

    }
    clickupload(e) {
        console.log(11111);
        console.log(e, 'eeeee');
        $('#file' + e).click();
    }

    del_img(prop, i) {
        let { lbimglist } = this.state
        if (prop == 'lb') {
            let lbhtml = []
            lbimglist.splice(i, 1)
            lbimglist.forEach((x, i) => {
                lbhtml.push(
                    <div className="lbimg">

                        <img className="img" src={x} />
                        <div className="imgmodel">
                            <div className="delbox">
                                <img src="./src/dellb.png" onClick={this.del_img.bind(this, 'lb', i)} />

                            </div>
                        </div>
                    </div>
                )
            })
            this.setState({ lbhtml, lbimglist })
        } else {
            this.setState({ imgsrc: '' })
        }

    }
    putswipervidlistdata(target, filedata, prop) {
        let imgsrc = '', that = this;
        imgsrc = target[0]
        console.log(imgsrc, 'imgsrc');
        that.setState({ [prop]: imgsrc, videofile: filedata, })
    }

    changefile(e){
        this.setState({chosefile:!0})``
       // console.log(e,'eee');
    }


   

    render() {
        let {  } = this.state;
        return (
            <div className="breadLinebox">
                <antd.Select showSearch  style={{ width: "100px" }}  >
                                                                                <option value={-1}>请选择 </option>
                                                                                <option value={1}>1 </option>
                                                                                <option value={2}>2 </option>
                                                                                <option value={3}>3 </option>
                                                                                <option value={34}>34 </option>
                                                                                <option value={35}>35 </option>
                                                                                </antd.Select>

               
            </div>


        )


    }
}





ReactDOM.render(
    <div>
        <div id='body'>
            <Main />
        </div>
    </div>
    // <Barrage/>
    , $('.container')[0]
)